const Koa = require('koa')
const path = require('path')
const KoaBody = require('koa-body')
const koaStatic = require('koa-static')
const parameter = require("koa-parameter")

const app = new Koa()

const errhandler = require('./errHandler')

const router = require('../router')

app.use(KoaBody({
  multipart: true,
  formidable: {
    // uploadDir: path.join(__dirname, '../upload'),
    keepExtensions: true,
  },
  parsedMethods: ['POST', 'PUT', 'PATCH', 'DELETE'],
}))
// 设置静态资源
app.use(koaStatic(path.join(__dirname, '../upload')))
// 格式校验
app.use(parameter(app))

app.use(router.routes()).use(router.allowedMethods())


// 同意错误处理函数
app.on('error', errhandler)

module.exports = app